package com.soc.erp.employee;

import java.util.List;

import org.nature.framework.annotation.Ask;
import org.nature.framework.annotation.Ctrl;
import org.nature.framework.annotation.Inject;
import org.nature.framework.annotation.Redirect;
import org.nature.framework.bean.JsonData;
import org.nature.framework.core.NatureCtrl;
import org.nature.framework.core.NatureMap;
import org.nature.framework.util.StringUtil;

import com.soc.erp.index.IService;

@Ctrl(namespace = "/employee")
public class EmployeeCtrl extends NatureCtrl {
	private static final long serialVersionUID = 1L;
	public Employee employee = new Employee();
	public String ask;
	@Inject
	IService iService;

	public NatureMap v = new NatureMap();
 
	@Ask
	public Object list() {
		String sql = "SELECT * from HR_Employee where 1 = 1";

		if (StringUtil.isNotBank(employee.getString(Employee.cEMName))) {
			sql += " and cEMName like '%" + employee.getString(Employee.cEMName) + "%'";
		}

		if (StringUtil.isNotBank(employee.getString(Employee.cEMNo))) {
			sql += " and cEMNo like '%" + employee.getString(Employee.cEMNo) + "%'";
		}

		/*if ((StringUtil.isNotBank(systemUser.getString(SystemUser.role_id))) && (!systemUser.getString(SystemUser.role_id).equals("-1"))) {
			sql += " and A.role_id = '" + systemUser.getString(SystemUser.role_id)+"'";
		}*/

		sql += " order by cEMNo";
		page = iService.page(page, sql);
		iService.jsPage(page, "jsPage");
		
		/*List<NatureMap> sexs = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'sex' order by c_key");
		page.put("sexs", sexs);*/
		
		/*List<NatureMap> level = iService.list("select * from tb_level");
		page.put("level", level);*/

		return "list.ftl";
	}

	@Ask
	public Object toOper() {
		/*List<NatureMap> role = iService.list("select * from tb_role");
		page.put("role", role);
*/
		if (StringUtil.isNotBank(employee.getString(Employee.cEMNo))) {
			iService.byId(employee);
			ask = "update";
		} else {
			ask = "insert";
		}
		
		/*List<NatureMap> sexs = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'sex' order by c_key");
		page.put("sexs", sexs);*/
		
		return "oper.ftl";
	}
	
	@Ask
	public Object toPerm() {
		/*String sql = "SELECT member_id, real_name, case when member_id in";
		sql += String.format(" (select browse_member_id from tb_member_perm where member_id = '%s') then 1 else 0 end as browse", member.getString(Member.member_id));
		sql += String.format(" FROM tb_member where member_id != '%s'", systemUser.getString(SystemUser.id));

		page.put("browses", iService.list(sql));*/

		return "perm.ftl";
	}

	@Ask
	public Object updatePerm() {

		/*if (StringUtil.isNotBank(systemUser.getString(SystemUser.member_id))) {
			String[] perm_ids = systemUser.getString(MemberPerm.browse_member_id).split(",");
			List<NatureMap> perms = iService.list(String.format("SELECT * FROM tb_member_perm WHERE member_id='%s'", systemUser.getString(SystemUser.id)));

			for (NatureMap perm : perms) {
				if (!Arrays.asList(perm_ids).contains(perm.getString("browse_member_id"))) {
					deleteMemberPerm(perm.getString("id"));

				}
			}

			for (String perm_id : perm_ids) {
				if (StringUtil.isNotBank(perm_id)) {
					insertMemberPerm(systemUser.getString(Member.member_id), perm_id);
				}
			}
		}*/
		return "list";
	}

	/*private void insertMemberPerm(String member_id, String browse_member_id) {
		String sql = String.format("SELECT COUNT(*) FROM tb_member_perm WHERE member_id='%s' AND browse_member_id='%s'", member_id, browse_member_id);
		Object ct = iService.uniqueValue(sql);
		if (CastUtil.castInt(ct) == 0) {
			MemberPerm memberPerm = new MemberPerm();
			memberPerm.put(MemberPerm.member_id, member_id);
			memberPerm.put(MemberPerm.browse_member_id, browse_member_id);
			memberService.insert(memberPerm);
		}
	}
*/
	/*private void deleteMemberPerm(String id) {
		MemberPerm memberPerm = new MemberPerm();
		memberPerm.put(MemberPerm.id, id);
		memberService.delete(memberPerm);
	}*/

	/*
	 * @Ask public Object insert(){ memberService.insert(member); return "list";
	 * }
	 * 
	 * @Ask public Object update(){ memberService.update(member); return "list";
	 * }
	 */
	@Ask
	@Redirect
	public Object delete() {
		/*systemUser = iService.byId(systemUser);
		systemUser.put("deleted", 1);
		iService.update(systemUser);*/
		// memberService.delete(member);
		return "list";
	}

	// 显示修改密码
	@Ask
	public Object show_change_password() {

		return "change-password.ftl";
	}

	// 修改密码
	@Ask
	public Object ajaxChangeSystemUserPassword() {

		/*String oldPass = v.getString("oldPassword");
		String newPass = v.getString("newPassword");

		systemUser.put(Employee.id, getSession().getAttribute("systemuser_id"));
		iService.byId(systemUser);*/
		/*if (!systemUser.getString(SystemUser.password).equals(iService.sha1(oldPass))) {
			return new JsonData(1);
		}*/
		/*if (!systemUser.getString(Employee.password).equals(oldPass)) {
			return new JsonData(1);
		}*/

		//systemUser.put(SystemUser.password, iService.sha1(newPass));//MD5
		//systemUser.put(Employee.password, newPass);
		//iService.update(systemUser);
		return new JsonData(0);
	}

	// 显示个人信息
	@Ask
	public Object show_user_info() {
		/*systemUser.put(Employee.id, getSession().getAttribute("systemuser_id"));
		iService.byId(systemUser);
		*/
		List<NatureMap> role = iService.list("select * from tb_role");
		page.put("role", role);
		
		List<NatureMap> sexs = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'sex' order by c_key");
		page.put("sexs", sexs);
		
		return "info.ftl";
	}

	// 修改个人信息
	@Ask
	public Object modify() {
		//iService.update(systemUser);
		return "show_user_info";
	}

	@Ask
	public Object ajaxCheckMemberValue() {
		/*String id = systemUser.getString(Employee.id);
		String name = systemUser.getString(Employee.login_name);
		String roleId = systemUser.getString(Employee.role_id);*/
	

		/*if (roleId.equals("-1")) {
			return new JsonData(1);
		}

		String sql = String.format("select count(*) from tb_system_user where login_name = '%s' and id <> '%s'", name, id);
		Object cnt = iService.uniqueValue(sql);
		if (CastUtil.castInt(cnt) > 0) {
			return new JsonData(2);
		}*/

		/*if (systemUser.containsKey(SystemUser.password) && StringUtil.isBank(systemUser.getString(SystemUser.password)))
			systemUser.remove(SystemUser.password);

		if (systemUser.containsKey(SystemUser.password))
			systemUser.put(SystemUser.password, iService.sha1(systemUser.getString(SystemUser.password)));*/
		/*if (ask.equals("insert")) {
			systemUser.put(Employee.role_id, "1");
			systemUser.put(Employee.password, "123456");
			systemUser.put(Employee.deleted, 0);
			iService.insert(systemUser);
		} else {
			iService.update(systemUser);
		}*/
		return new JsonData(0);
	}
}
